import pandas as pd
import logging
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, classification_report

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger()

logger.info("1. 加载数据")
data = pd.read_excel('homework\\covid-19 symptoms dataset.xlsx')

# 2. 数据预处理
logger.info("2. 数据预处理")
X = data.drop('infectionProb', axis=1)  # 特征
y = data['infectionProb']  # 标签

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
logger.info(f"训练集大小: {X_train.shape}, 测试集大小: {X_test.shape}")

# 3. 创建逻辑回归模型
logger.info("3. 创建逻辑回归模型")
model = LogisticRegression()

# 4. 训练模型
logger.info("4. 训练模型")
model.fit(X_train, y_train)

# 5. 模型评估
logger.info("5. 模型评估")
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
logger.info(f'模型准确率: {accuracy:.4f}')
logger.info(f'分类报告:\n{classification_report(y_test, y_pred)}')

# 6. 输出完成日志
logger.info("实验完成")
